layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    var status = $("#status").val();
    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/coupon/jsonList',
        cellMinWidth : 95,
        where : {status:status},
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {field: 'cardNo', title: '会员ID',align:'center'},
            {field: 'memberName', title: '姓名',align:'center'},
            {field: 'money', title: '条件',align:'center',templet:function (d) {
                return "满"+d.fullMoney+"减"+d.reduceMoney;
            }},
            {field: 'startTime', title: '开始时间', align:'center', width:170, templet:function(d){
                return d.startTime;
            }},
            {field: 'endTime', title: '结束时间', align:'center', width:170, templet:function(d){
                return d.endTime;
            }},
            {field: 'status', title: '类型',align:'center',templet:function(d){
                if(d.status=="1"){
                    return "未使用";
                }else if(d.status=="2"){
                    return "已使用";
                }
            }},
            {field: 'validState', title: '是否有效',align:'center',templet:function(d){
                if(d.validState=="1"){
                    return "有效";
                }else if(d.validState=="2"){
                    return "过期";
                }
            }},
            {field: 'createTime', title: '创建时间', align:'center', width:170, templet:function(d){
                return d.createTime;
            }}/*,
            {title: '操作',width:170,align:"center", templet:function(d){
                return '<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>';
            }}*/
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                memberName: $("#memberName").val(),
                cardNo:$("#cardNo").val(),
                status: $("#status").val()
            }
        })
    });

    $(".add_btn").on("click",function(){
        // 提现
        addEntity("");
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        if(params==null||params==""){
            title="添加"
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            content : "/coupon/selectEntity",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    }

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?id="+data.id);
        } 
    });

})